#include "StdAfx.h"
#include ".\viewrotateyawpitch.h"

#include <matrix.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

ViewRotateYawPitch::ViewRotateYawPitch(void)
{
	m_bEyeLight = false;
	m_nFlagsLast = 0;
	m_nAxisRot1 = 1;
	m_nAxisRot2 = 2;
	m_nRotDir1 = m_nRotDir2 = 1;
}

ViewRotateYawPitch::~ViewRotateYawPitch(void)
{
}

static GLfloat ypLightpos[] = { 0.0f, 0.0f, 1.0f, 0.0f};

static GLfloat ypLightAmbient[] = { 0.2f, 0.2f, 0.2f, 1.0f };
static GLfloat ypLightDiffuse[] = { 0.7f, 0.7f, 0.7f, 1.0f };
static GLfloat ypLightSpecular[] = { 0.9f, 0.9f, 0.9f, 1.0f };
static GLfloat ypMaterialColor[] = { 0.8f, 0.0f, 0.0f };

void ViewRotateYawPitch::Draw()
{
	 
	
	

	


	if(m_bEyeLight)
	{
		mdsVector<3, double> vecLookDir = GetLookDir();
		//ypLightpos[0] = vecLookDir[0];
		//ypLightpos[1] = vecLookDir[1];
		//ypLightpos[2] = vecLookDir[2];
		//ypLightpos[3] = 0.0f;
		glLightfv(GL_LIGHT0,GL_POSITION, ypLightpos);
		glLightfv(GL_LIGHT0, GL_AMBIENT, ypLightAmbient);
		glLightfv(GL_LIGHT0, GL_DIFFUSE, ypLightDiffuse);
		glLightfv(GL_LIGHT0, GL_SPECULAR, ypLightSpecular);
		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);
		//glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,ypLightSpecular);
		//glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, ypMaterialColor);
		//glMateriali(GL_FRONT_AND_BACK, GL_SHININESS,128); 
	}


	{
		// glMatrixMode( GL_PROJECTION );
		SetGlViewTo();
	}
}

bool ViewRotateYawPitch::OnMouseMove(UINT nFlags, double lfX, double lfY)
{
	bool bResult = true;
	mdsVector<2, double> p2CurrPos;
	p2CurrPos[0] = lfX;
	p2CurrPos[1] = lfY;

	//# MK_LBUTTON   Set if the left mouse button is down.
	//# MK_MBUTTON   Set if the middle mouse button is down.
	//# MK_RBUTTON   Set if the right mouse button is down.

	if(nFlags & m_nFlagsLast & MK_RBUTTON)
	{
		MoveViewBack((p2CurrPos[1]-m_p2MousePosLast[1])*(1+GetViewBack()));
		bResult = true;
	}

	if(nFlags & m_nFlagsLast & MK_LBUTTON)
	{
		mdsMatrix<3, double> rotMatGlbl(false);
		mdsMatrix<3, double> rotMatLcl(false);

		

		rotMatLcl = matrixGlobals::GetAxisRot(m_nAxisRot1, m_nRotDir1*(p2CurrPos[1]-m_p2MousePosLast[1]));
		rotMatGlbl = matrixGlobals::GetAxisRot(m_nAxisRot2, m_nRotDir2*(p2CurrPos[0]-m_p2MousePosLast[0]));
		RotateByLcl(rotMatLcl);
		RotateBy(rotMatGlbl);
		// TRACE("Rotating\n");
		bResult = true;
	}



	
	

	m_nFlagsLast = nFlags;
	m_p2MousePosLast[0] = lfX;
	m_p2MousePosLast[1] = lfY;

	return bResult;
}
